﻿namespace Events;

public class GoldPriceReader:IObservable<decimal>
{
    public event PriceRead? PriceRead;
    
    private int _currentGoldPrice;
    private readonly List<IObserver<decimal>> _observers = new();
   

    public void ReadCurrentPrice()
    {
        var currentGoldPrice = new Random().Next(
            20_000, 50_000);
        
        //NotifyObservers();
        OnPriceRead(currentGoldPrice);
    }

    private void OnPriceRead(decimal price)
    {
        //PriceRead(price);
        PriceRead?.Invoke(price);
    }

    public void AttachObserver(IObserver<decimal> observer)
    {
        _observers.Add(observer);
    }

    public void DetachObserver(IObserver<decimal> observer)
    {
        _observers.Remove(observer);
    }

    public void NotifyObservers()
    {
        foreach (var observer in _observers)
        {
            observer.Update(_currentGoldPrice);
        }
    }
}